package utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class InputStreamUtil {
	public static final String CHARSET_OPENDATA = "UTF-8";

	public static String readInputStream(InputStream is) throws IOException {
		BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName(CHARSET_OPENDATA)));
		StringBuilder sb = new StringBuilder();
		try {
			int cp;
			while ((cp = rd.read()) != -1) {
				sb.append((char) cp);
			}
			return sb.toString();
		} catch(IOException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	public static void closeInputStream(InputStream is) {
		try {
			if(is != null) {
				is.close();
			}
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
}
